package com.ztf.room.controller;

import com.github.pagehelper.PageInfo;
import com.ztf.room.bean.Evaluates;
import com.ztf.room.bean.Student;
import com.ztf.room.bean.Teachers;
import com.ztf.room.service.TeacherService;
import com.ztf.room.utils.Result;
import com.ztf.room.utils.ResultInfo;
import com.ztf.room.utils.SessionConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    // 评价展示
    @RequestMapping("/evaluation")
    public String evaluation(){
        return "student/evaluation.jsp";
    }

    // 评价
    @RequestMapping("/evaluationRate")
    public String evaluationRate(){
        return "student/page/evaluate.html";
    }

    // 获取评价列表
    @RequestMapping("/getTeacherEvaluates")
    @ResponseBody
    public Result getTeacherEvaluates(HttpSession session,int page,int limit){
        Object attribute = session.getAttribute(SessionConstant.STUDENT_NAME);

        if (attribute!=null){
            // 学生登录 ,只能查看自己的评测过的记录
             Student student = (Student) attribute;
            PageInfo<Evaluates>  list =  teacherService.getEvauatesByStu(student.getStudentId(),page,limit);
            return ResultInfo.success(list);
        }else {
            // 老师登录,查看与自己相关的评分
            Teachers teachers = (Teachers) session.getAttribute(SessionConstant.TEACHER_NAME);
            PageInfo<Evaluates> list = teacherService.getEvauatesByTea(teachers.getTeacherId(),page,limit);
            return ResultInfo.success(list);
        }
    }

    // 学生提交评价
    @RequestMapping("/saveEvaluation")
    @ResponseBody
    public Result saveEvaluation(Evaluates evaluates ,HttpSession session){
        Student student = (Student) session.getAttribute(SessionConstant.STUDENT_NAME);
        boolean  flag = teacherService.saveEvaluation(evaluates);
        return ResultInfo.success("");
    }


    @RequestMapping("/teacherEval")
    public String teacherEval(){
        return "student/teacherAddEval.jsp";
    }

    // 管理员发布老师评价
    @RequestMapping("/releaseEval")
    @ResponseBody
    public Result  releaseEval(@RequestBody ArrayList<Teachers> list){
        // 发布 老师评价
        System.out.println(list);
        boolean flag = teacherService.releaseEval(list);
        return ResultInfo.success();
    }

    // 管理员获取老师的列表
    @RequestMapping("/getTeachers")
    @ResponseBody
    public Result getTeachers(int page,int limit){
        PageInfo<Teachers> pageInfo = teacherService.getTeachers(page,limit);
        return ResultInfo.success(pageInfo);
    }

}

